package com.asiainfo.kbisheet.util;

import groovy.util.logging.Slf4j

import java.util.List

import com.asiainfo.kbisheet.vo.AllExportVO
import com.asiainfo.kbisheet.vo.DimVO
import com.asiainfo.kbisheet.vo.FromToDateVO
import com.asiainfo.kbisheet.vo.KbsSheetVO
import com.asiainfo.kbisheet.vo.LabelValueVO
import com.asiainfo.kbisheet.vo.QueryVO
import com.asiainfo.kbisheet.vo.SettingVO
import com.asiainfo.kbisheet.vo.UserColumnHiddenVO

@Slf4j
abstract class BUCommon implements BU{
	SettingVO settingVO = Setting.getSettingVO();
	DBUFactory dbuFactory = new DBUFactory();
	
	public boolean allExport(AllExportVO allExportVO,QueryVO queryVO){
		false;
	}
	
	public String decrypt(String key,String src){
		null;
	}
	
	public List<KbsSheetVO> getAllKbsSheetVOList(){
		DBU kbsBaseDBU = dbuFactory.createKbsBaseDBU();
		List<KbsSheetVO> list
		try{
			list = kbsBaseDBU.getAllKbsSheetVOList();
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}		
		list;
	}
	
	public FromToDateVO getDimDate(DimVO dimVO){
		log.info "得到时间维表数据 ..."
		DBU kbsBaseDBU = dbuFactory.createDBU(dimVO.sourceKey)
		FromToDateVO dimDate
		try{
			dimDate = kbsBaseDBU.getDimDate(dimVO);
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}
		
		dimDate
	}
	
	public List<LabelValueVO> getDimList(DimVO dimVO){
		log.info "得到列表维表数据 ..."
		DBU kbsBaseDBU = dbuFactory.createDBU(dimVO.sourceKey)
		List<LabelValueVO> dimList
		try{
			dimList = kbsBaseDBU.getDimList(dimVO);
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}
		
		dimList
	}
	
	public String getDataXML(QueryVO queryVO){
		log.info "得到表单内容XML ..."
		DBU kbsBaseDBU = dbuFactory.createDBU(queryVO.sourceKey)
		String dataXML
		try{
			dataXML = kbsBaseDBU.getDataXML(queryVO)
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}
		dataXML
	}
	
	public List<String> loadUserColumnSet(String userId,String sheetId){
		log.info "加载列设置，sheetId = [" + sheetId + "]，  userId=["+ userId +"]..."
		DBU kbsBaseDBU = dbuFactory.createKbsBaseDBU();
		List<String> userColumnSet
		try{
			userColumnSet = kbsBaseDBU.loadUserColumnSet(userId, sheetId);
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}		
		userColumnSet
	}
	
	public boolean saveUserColumnSets(UserColumnHiddenVO userColumnHiddenVO){
		log.info "保存列设置..."
		boolean result
		DBU kbsBaseDBU = dbuFactory.createKbsBaseDBU();
		try{
			result = kbsBaseDBU.saveUserColumnSets(userColumnHiddenVO)
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}
		result
	}
	
	public String loadXmlContent(String xmlName){
		log.info "加载表单XML内容， xmlName = [" + xmlName + "] ..."
		String content = (new File(settingVO.getFilesRoot() + xmlName)).getText();
		content;
	}
	
	public String getColumnsXML(String dataBaseName,String tableName){
		log.info "得到数据库表列的XML信息 ..."
		DBU kbsBaseDBU = dbuFactory.createKbsBaseDBU();
		String columnsXML
		try{
			columnsXML = kbsBaseDBU.getColumnsXML(dataBaseName, tableName)
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}
		columnsXML
	}
	
	public KbsSheetVO loadKbsSheetVO4Id(String sheetId){
		log.info "加载表单，sheetId = [" + sheetId + "] ..."
		DBU kbsBaseDBU = dbuFactory.createKbsBaseDBU();
		KbsSheetVO kbsSheetVO4Id
		try{
			kbsSheetVO4Id = kbsBaseDBU.loadKbsSheetVO4Id(sheetId);
		}
		catch(Throwable t){
			log.error(t,t)
			throw t
		}
		finally{
			kbsBaseDBU.close()
		}
		kbsSheetVO4Id
	}
}
